<template>

  <el-card>

    <!--头部-->
    <div style="min-height: 220px; margin-bottom: 5px">
      <el-row>
        <el-col :span="24">

          <div class="box-content" >

            <!-- 图片 + 文字的 flex 容器 -->
            <div class="" style="display: flex; gap: 10px; align-items: flex-start;">
              <!--图片-->
              <div>
                <el-image class="component-ico" fit="contain" :src="product.icoUrl || require('@/assets/images/ico/product2.png')" />
              </div>

              <!--图片左边的文字-->
              <div >

                <div style="display: flex; gap: 10px; align-items: center; min-width: 500px">
                  <h2>{{ product.name }}</h2>

                  <el-tag size="mini" type="warning" v-if="product.status === 1">立项</el-tag>
                  <el-tag size="mini" v-else-if="product.status === 2">研发</el-tag>
                  <el-tag size="mini" type="danger" v-else-if="product.status === 3">部署</el-tag>
                  <el-tag size="mini" type="success" v-else-if="product.status === 4">维护</el-tag>
                  <el-tag size="mini" type="info" v-else-if="product.status === 99">废弃</el-tag>
                </div>



                <el-row >
                  <el-col :span="12">
                    重要性：
                    <el-tag size="mini" type="info" v-if="product.importance === 1">辅助产品</el-tag>
                    <el-tag size="mini" type="success" v-else-if="product.importance === 2">普通产品</el-tag>
                    <el-tag size="mini" type="warning" v-else-if="product.importance === 3">重点产品</el-tag>
                    <el-tag size="mini" type="danger" v-else-if="product.importance === 4">核心产品</el-tag>
                  </el-col>
                  <el-col :span="12">
                    公共产品：
                    <el-tag size="mini" type="danger" v-if="product.common === 1">公共产品</el-tag>
                    <el-tag size="mini" type="info" v-else-if="product.common === 0">非公共产品</el-tag>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 10px">
                  <el-col :span="12">
                    负责人：<span>{{product.directorName}}</span>
                  </el-col>
                  <el-col :span="12">
                  </el-col>
                </el-row>

              </div>

              <!--操作按钮-->
              <div style="margin-left: auto; display: flex; gap: 10px;">
                <el-button @click="saveOpsProductDependBefore()" type="warning" :size="layoutCss.size">添加绑定</el-button>
                <!--<el-button @click="delTips(site)" type="danger" :size="layoutCss.size">删除</el-button>-->
              </div>

            </div>

            <p style="margin-top: 10px; margin-bottom: 10px;">简介：</p>
            <p class="description">{{ product.briefIntroduction }}</p>
          </div>

        </el-col>
      </el-row>

      <!--分割线-->
      <el-divider></el-divider>
    </div>








    <!--中间部分-->
    <el-row :gutter="10">


      <!--中间左边-->
      <el-col :span="14">

        <el-tabs v-model="elTab.activeName" type="card" @tab-click="handleTabClick" style="min-height: 640px; max-height: 640px; ">
          <el-tab-pane label="应用" name="appDepend">

            <div class="container" v-if="elTab.activeName === 'appDepend'">

              <!--一个小框框-->
              <div class="box" v-for="(item, index) in tableData" :key="index">
                <div class="version-tag2 component-title" v-if="item.necessary === 1">必须应用</div>
                <div class="version-tag component-title" v-if="item.necessary === 0">非必须应用</div>
                <div class="box-content" >

                  <el-row :gutter="10">
                    <el-col :span="9">
                      <div class="block">
                        <el-image class="component-ico" fit="contain" :src="(item.opsApp == null || item.opsApp.icoUrl) || getDefaultIcon(item)" />
                      </div>
                    </el-col>
                    <el-col :span="15">

                      <el-row :gutter="10">
                        <el-col :span="24">
                          <h3 class="component-title"  @click="openAppLayout(item)">{{item.opsApp.name}}</h3>
                        </el-col>
                      </el-row>

                      <!--<el-row>-->
                      <!--  <el-col :span="8">支持环境：</el-col>-->
                      <!--  <el-col :span="16">-->
                      <!--    <span>{{item.osStr}}</span>-->
                      <!--  </el-col>-->
                      <!--</el-row>-->

                      <el-row >
                        <el-col :span="8">状态：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="warning" v-if="item.opsApp.state === 1">立项</el-tag>
                          <el-tag size="mini" v-else-if="item.opsApp.state === 2">开发</el-tag>
                          <el-tag size="mini" type="success" v-else-if="item.opsApp.state === 4">维护</el-tag>
                          <el-tag size="mini" type="info" v-else-if="item.opsApp.state === 99">废弃</el-tag>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px">
                        <el-col :span="8">类型：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="success" v-if="item.opsApp.type === 1">app</el-tag>
                          <el-tag size="mini" type="warning" v-else-if="item.opsApp.type === 2">前端</el-tag>
                          <el-tag size="mini" v-else-if="item.opsApp.type === 3">后端</el-tag>
                          <el-tag size="mini" type="danger" v-else-if="item.opsApp.type === 4">小程序</el-tag>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px">
                        <el-col :span="8">安装数量：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="warning">{{item.installNum}}</el-tag>
                        </el-col>
                      </el-row>


                    </el-col>
                  </el-row>

                  <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
                  <p class="description" @click="openAppLayout(item)" >{{item.opsApp.briefIntroduction}}</p>


                  <!-- 固定在右下角的按钮组 -->
                  <div class="button-group">
                    <el-button type="primary" size="mini" @click="updOpsProductDepend(item)"  >转换</el-button>
                    <el-button type="danger" size="mini" @click="delTips(item)"  >解绑</el-button>
                  </div>

                </div>
              </div>
            </div>

          </el-tab-pane>


          <el-tab-pane label="组件" name="componentDepend">

            <div class="container" v-if="elTab.activeName === 'componentDepend'">

              <!--一个小框框-->
              <div class="box" v-for="(item, index) in tableData" :key="index">
                <div class="version-tag2 component-title" v-if="item.necessary === 1">必须组件</div>
                <div class="version-tag component-title" v-if="item.necessary === 0">非必须组件</div>
                <!--<div class="version-tag component-title" v-if="item.opsComponent.mainVersion">v. {{item.opsComponent.mainVersion}}</div>-->
                <div class="box-content" >

                  <el-row :gutter="10">
                    <el-col :span="9">
                      <div class="block">
                        <el-image class="component-ico" fit="contain" :src="item.opsComponent.icoUrl || require('@/assets/images/ico/app.png')" />
                      </div>
                    </el-col>
                    <el-col :span="15">

                      <el-row :gutter="10">
                        <el-col :span="24">
                          <h3 class="component-title"  @click="openComponentLayout(item)">{{item.opsComponent.name}}</h3>
                        </el-col>
                      </el-row>

                      <el-row>
                        <el-col :span="8">支持环境：</el-col>
                        <el-col :span="16">
                          <span>{{item.opsComponent.osStr}}</span>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px">
                        <el-col :span="8">安装数量：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="warning">{{item.installNum}}</el-tag>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px; height: 20px; " >
                        <el-col :span="8">官网：</el-col>
                        <el-col :span="16">
                          <el-link class="component-title" type="primary" target="_blank" :href="item.opsComponent.officialWebUrl">
                            {{ item.opsComponent.officialWebUrl }}
                          </el-link>
                        </el-col>
                      </el-row>

                    </el-col>
                  </el-row>

                  <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
                  <p class="description" @click="openComponentLayout(item)" >{{item.opsComponent.briefIntroduction}}</p>


                  <!-- 固定在右下角的按钮组 -->
                  <div class="button-group">
                    <el-button type="primary" size="mini" @click="updOpsProductDepend(item)"  >转换</el-button>
                    <el-button type="danger" size="mini" @click="delTips(item)"  >解绑</el-button>
                  </div>

                </div>
              </div>
            </div>

          </el-tab-pane>


          <el-tab-pane label="依赖产品" name="productDepend">

            <div class="container" v-if="elTab.activeName === 'productDepend'">

              <!--一个小框框-->
              <div class="box" v-for="(item, index) in tableData" :key="index">
                <div class="version-tag2 component-title" v-if="item.necessary === 1">必须产品</div>
                <div class="version-tag component-title" v-if="item.necessary === 0">非必须产品</div>
                <div class="box-content" >

                  <el-row :gutter="10">
                    <el-col :span="9">
                      <div class="block">
                        <el-image class="component-ico" fit="contain" :src="(item.opsProduct && item.opsProduct.icoUrl) || require('@/assets/images/ico/product2.png')" />
                      </div>
                    </el-col>
                    <el-col :span="15">

                      <el-row :gutter="10">
                        <el-col :span="24">
                          <h3 class="component-title"  @click="openProductLayout(item)">{{item.opsProduct.name}}</h3>
                        </el-col>
                      </el-row>

                      <el-row >
                        <el-col :span="8">状态：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="warning" v-if="item.opsProduct.status === 1">立项</el-tag>
                          <el-tag size="mini" v-else-if="item.opsProduct.status === 2">研发</el-tag>
                          <el-tag size="mini" type="danger" v-else-if="item.opsProduct.status === 3">部署</el-tag>
                          <el-tag size="mini" type="success" v-else-if="item.opsProduct.status === 4">维护</el-tag>
                          <el-tag size="mini" type="info" v-else-if="item.opsProduct.status === 99">废弃</el-tag>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px">
                        <el-col :span="8">重要性：</el-col>
                        <el-col :span="16">
                          <el-tag size="mini" type="success" v-if="item.opsProduct.importance === 1">辅助产品</el-tag>
                          <el-tag size="mini" type="warning" v-else-if="item.opsProduct.importance === 2">普通产品</el-tag>
                          <el-tag size="mini" v-else-if="item.opsProduct.importance === 3">重点产品</el-tag>
                          <el-tag size="mini" type="danger" v-else-if="item.opsProduct.importance === 4">核心产品</el-tag>
                        </el-col>
                      </el-row>

                      <el-row style="margin-top: 5px">
                        <el-col :span="8">负责人：</el-col>
                        <el-col :span="16">
                          <div>{{item.opsProduct.directorName}}</div>
                        </el-col>
                      </el-row>


                    </el-col>
                  </el-row>

                  <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
                  <p class="description" @click="openProductLayout(item)" >{{item.opsProduct.briefIntroduction}}</p>


                  <!-- 固定在右下角的按钮组 -->
                  <div class="button-group">
                    <el-button type="primary" size="mini" @click="updOpsProductDepend(item)"  >转换</el-button>
                    <el-button type="danger" size="mini" @click="delTips(item)"  >解绑</el-button>
                  </div>

                </div>
              </div>
            </div>

          </el-tab-pane>


        </el-tabs>

        <!--分页部分-->
        <el-pagination
          style="width:33%; margin-right:10px; margin-top:10px;"
          background
          layout="total, prev, pager, next, jumper"
          @current-change="pageCurrentChange"
          :page-size="pageConfig.size"
          :current-page="pageConfig.current"
          :pager-count="5"
          :total="pageConfig.total">
        </el-pagination>

      </el-col>



      <!--中间右边，文件树-->
      <el-col :span="10">
        <OpsCommonUseList ref="OpsCommonUseListRef" bind-type="product" :bind-id="this.$route.query.productId"/>
      </el-col>

    </el-row>


    <OpsProductDependSave  ref="OpsProductDependSaveRef" @getListPage="getListPage"/>

  </el-card>

</template>

<script>

import http from '@/utils/http'
import {clearInvalid} from "@/utils/utils"
import OpsCommonUseList from "../common/OpsCommonUseList.vue";
import OpsProductDependSave  from './OpsProductDependSave.vue'

export default {
  name: "ProductLayout",
  components: {
    OpsCommonUseList,
    OpsProductDependSave,
  },
  data() {
    return {
      layoutCss :{
        size: 'mini',
      },
      // 列表的尺寸
      size: 'small',
      // 描述列表的css
      CS: {
        // 'text-align': 'center',  //文本居中
        'min-width': '250px',   //最小宽度
        'word-break': 'break-all'  //过长时自动换行
      },
      // 描述列表的css
      LS: {
        // 'color': '#000',
        // 'text-align': 'center',
        'font-weight': '600',
        // 'height': '40px',
        // 'background-color': 'rgba(255, 97, 2, 0.1)',
        'min-width': '110px',
        'word-break': 'keep-all'
      },
      // 当前项目
      product:{
        id: null,
        ico: null,
        name: null,
        status: null,
        importance: null,
        directorName: null,
        common: null,
      },
      // 标签页信息
      elTab:{
        // 被选中的标签
        activeName: 'appDepend',
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 10,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
      // 查询参数-依赖查询
      queryParam:{
        productId: null,
        necessary: null,
        bindId: null,
        type: 2,
      },
    }
  },
  methods: {
    /**
     * 渲染图片
     * 只能这样做，图片这里不支持非固定地址返回
     * @param item 目标数据
     */
    getImageUrlInfo(item) {
      if (!item || !item.ico || item.ico === 'null') {
        // 判空或无效，直接显示默认图
        this.$set(item, 'icoUrl', null);
        return;
      }

      http.getImageUrl(item.ico)
        .then(url => {
          // 异步赋值，触发响应式更新
          this.$set(item, 'icoUrl', url);
        })
        .catch(() => {
          // 下载失败 fallback 默认图
          this.$set(item, 'icoUrl', null);
        });
    },
    /**
     * 打开产品详情
     * @param obj 产品信息
     */
    openProductLayout(obj){
      // this.$router.push({path: '/toProductLayout', query: {productId: obj.opsProduct.id}})
    },
    /**
     * 打开组件详情
     * @param obj 组件信息
     */
    openComponentLayout(obj){
      this.$router.push({path: '/toComponentLayout', query: {componentId: obj.opsComponent.id}})
    },
    /**
     * 打开组件详情
     * @param obj 组件信息
     */
    openAppLayout(obj){
      this.$router.push({path: '/toAppLayout', query: {appId: obj.opsApp.id}})
    },
    /**
     * 获取默认的图标-应用
     * @param type
     * @returns {*}
     */
    getDefaultIcon(item) {
      if (item == null || item.opsApp == null){
        return require('@/assets/images/ico/app2.png');
      }
      let type = item.opsApp.type
      switch(type) {
        case 1:
          return require('@/assets/images/ico/app3.png');
        case 2:
          return require('@/assets/images/ico/web.png');
        case 3:
          return require('@/assets/images/ico/java.png');
        case 4:
          return require('@/assets/images/ico/smallProgram.png');
        default:
          return require('@/assets/images/ico/app2.png');
      }
    },
    /**
     * 添加绑定关系之前
     */
    saveOpsProductDependBefore(){
      this.$refs.OpsProductDependSaveRef.init('save', this.product)
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getListPage()
    },
    /**
     * 标签页被点击的时候
     * @param tab
     * @param event
     */
    handleTabClick(tab, event){
      // console.log(this.elTab.activeName)
      this.pageConfig.current = 1
      this.pageConfig.size = 10
      this.tableData = []
      this.getListPage()
    },
    /**
     * 分页查询列表-查询依赖
     */
    getListPage() {
      if (this.elTab.activeName === 'appDepend'){
        this.queryParam.type = 2
      } else if (this.elTab.activeName === 'componentDepend'){
        this.queryParam.type = 3
      } else if (this.elTab.activeName === 'productDepend'){
        this.queryParam.type = 1
      }
      this.queryParam.current = this.pageConfig.current
      this.queryParam.size = this.pageConfig.size
      this.queryParam.productId = this.$route.query.productId

      http.post("/opsProductDepend/getListPage", this.queryParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records
        // 异步加载每个图片
        this.tableData.forEach(item => {
          if (item.type === 2){
            this.getImageUrlInfo(item.opsApp)
          } else if (item.type === 3){
            this.getImageUrlInfo(item.opsComponent)
          } else {
            this.getImageUrlInfo(item.opsProduct)
          }
        })


      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 查询当前产品
     */
    getCurrentProduct() {
      let query = {
        current: 1,
        size: 1,
        id: this.$route.query.productId,
      }
      http.post("/opsProduct/getListPage", query).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        // 表格数据
        if (data.records != null && data.records.length > 0) {
          this.product = data.records[0]
        }

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 删除之前的提示框
     * @param obj 要删除的数据
     */
    delTips(obj) {
      this.$confirm('是否执行删除, 删除后无法恢复，是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      }).then(() => {
        // 确定删除
        this.delOpsProductDepend(obj)

      }).catch(() => {
        // 取消删除
      });
    },
    /**
     * 删除
     * @param obj 要删除的数据
     */
    delOpsProductDepend(obj) {
      http.post("opsProductDepend/delOpsProductDepend", {
        id: obj.id,
      }).then(response => {
        let res = response.data;
        this.getListPage()
        this.$message.success("删除成功");

      }).catch(error => {
        this.$message.error (error.message)
      })
    },
    /**
     * 更新
     * @param item 当前对象
     */
    updOpsProductDepend(item){
      clearInvalid(item)
      if (item.necessary === 1){
        item.necessary = 0
      } else {
        item.necessary = 1
      }
      http.post("/opsProductDepend/updOpsProductDepend", item).then(response => {
        let res = response.data;
        this.$message.success ("更新成功");
        this.handleTabClick()

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
  },
  mounted() {
    this.$store.commit('navigationListChange', [
      {name: '产品列表', url: '/toProductList', query: {}},
      {name: '产品详情', url: '/toProductLayout', query: {productId: this.$route.query.productId}},
    ])

    this.getCurrentProduct()
    this.handleTabClick()
  },
}
</script>

<style scoped>


.component-ico {
  width: 126px;
  height: 110px;
}

.box-content .description {
  margin-top: 0px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 显示3行 */
  -webkit-box-orient: vertical;
  cursor: pointer;
}



/**
====== 一个方框 start ===
 */

.container {
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  gap: 20px; /* 小框框之间的间距 */
  padding: 20px;
  min-height: 500px;
  max-height: 500px;
  overflow: auto;
}

.component-ico {
  width: 126px;
  height: 110px;
}

.box {
  width: 380px; /* 固定宽度 */
  height: 270px; /* 固定高度 */
  min-height: 270px;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /*overflow: hidden;*/
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  position: relative; /* 添加这个以便version-tag可以绝对定位 */
  overflow: hidden; /* 保持hidden防止内容溢出 */
}

.box-content {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.box-content .description {
  margin-top: 0px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 显示3行 */
  -webkit-box-orient: vertical;
  cursor: pointer;
  margin-bottom: 6px;
}

.component-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  cursor: pointer;
}

/* 角标 start */

.version-tag {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  background: #D4E6F1;
  /*box-shadow: 0 0 0 999px #8FBCDA;*/
  box-shadow: 0 0 0 999px #D4E6F1;
  clip-path: inset(0 -100%);
}

.version-tag2 {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  background: #FFE14C;
  /*box-shadow: 0 0 0 999px #8FBCDA;*/
  box-shadow: 0 0 0 999px #FFE14C;
  clip-path: inset(0 -100%);
}


/* 角标 end */

/* 按钮组固定在右下角 */
.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: auto; /* 关键属性，使按钮组固定在底部 */
  /*padding-top: 10px;*/
}

/**
====== 一个方框 end ===
 */

</style>
